package com.lolong.hic;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class InformationActivity extends Activity implements OnClickListener {
	
	private TextView storeNameTV, floorTV, landmarkTV, priceRangeTV;
	private SQLiteConnector connector; 
	private ImageButton favoriteButton, backBtn;
	
	private String storeName;
	private ArrayList<String> estabInfoArrayList;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.information);
		
		initComponents();
	}
	
	
	public void initComponents() {
		
		Bundle extras = getIntent().getExtras();
		
		estabInfoArrayList = new ArrayList<String>();
		connector = new SQLiteConnector(this);
		
		storeNameTV = (TextView) findViewById(R.id.storeNameInfoTextView);
		floorTV = (TextView) findViewById(R.id.floorLevelInfoTextView);
		landmarkTV = (TextView) findViewById(R.id.landmarkInfoTextView);
		priceRangeTV = (TextView) findViewById(R.id.priceRangeInfoTextView);
		
		favoriteButton = (ImageButton) findViewById(R.id.favoriteInfoButton);
		initFavoriteButton();
		backBtn = (ImageButton) findViewById(R.id.back);
		backBtn.setOnClickListener(this);
		
		storeName = extras.getString("establishmentName");
		storeNameTV.setText(storeName);
		
		estabInfoArrayList.addAll(connector.getEstablishmentInfo(storeName));
		
		floorTV.setText(estabInfoArrayList.get(0));
		landmarkTV.setText(estabInfoArrayList.get(1));
		priceRangeTV.setText(estabInfoArrayList.get(2));
	}
	
	public void initFavoriteButton() {
		
		if(checkIfFavorite() == 0) {
			favoriteButton.setImageResource(android.R.drawable.btn_star_big_off);
		} else {
			favoriteButton.setImageResource(android.R.drawable.btn_star_big_on);
		}
		
		favoriteButton.setOnClickListener(this);
	}
	
	
	public int checkIfFavorite() {
		
		//0 not, 1 favorite
		int isFavorite = 0;
		
		isFavorite = connector.getIsFavorite(storeName);
		
		return isFavorite;
	}
	
	public void setIsFavorite() {
		
		connector.setFavorite(storeName);
		
		if(checkIfFavorite() == 0) {
			favoriteButton.setImageResource(android.R.drawable.btn_star_big_off);
		} else {
			favoriteButton.setImageResource(android.R.drawable.btn_star_big_on);
		}
	}

	
	@Override
	public void onClick(View v) {

		switch(v.getId()) {
		
		case R.id.favoriteInfoButton:
			setIsFavorite();
			break;
		
		case R.id.back:
			Intent intent = new Intent(InformationActivity.this, CategoryActivity.class);
			startActivity(intent);
			break;
		
		}
		
	}
}
